import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Address } from '../entities/address.entity';

@Injectable()
export class AddressService {
  constructor(
    @InjectRepository(Address)
    private readonly addressRepository: Repository<Address>,
  ) {}
  async createAddress(address: Address): Promise<Address> {
    return this.addressRepository.save(address);
  }
  async getAddress(id: number): Promise<Address> {
    let address = await this.addressRepository.query(
      'select * from address where id = ' + id,
    );
    if (address.length > 0) {
      return address[0];
    } else {
      throw {
        statusCode: 400,
        message: '地址记录不存在',
      };
    }
  }

  /**
   * Get all addresses
   */
  async getAddresses(): Promise<Address[]> {
    return this.addressRepository.find();
  }

  /**
   * Update an address
   * @param id
   * @param address
   */
  async updateAddress(id: number, address: Address): Promise<Address> {
    return this.addressRepository.save(address);
  }
  // async deleteAddress(id: number): Promise<Address> {
  //   return this.addressRepository.remove(id);
  // }
}
